package com.cardbaobao.cardbabyclient.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.ui.fragment.CreditFragment;

/**
 * Created by Jiangzx on 14:17.
 */

public class HotRecLabelDelegateAdapter extends DelegateAdapter.Adapter {
    private int mPosition;//位置 左边、右边
    private String mLabelName;
    private LayoutHelper mLayoutHelper;
    public HotRecLabelDelegateAdapter(LayoutHelper layoutHelper,String labelName,int position){
        mLayoutHelper=layoutHelper;
        mLabelName=labelName;
        mPosition=position;
    }
    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).
                inflate(R.layout.item_credit_card_hot_rec_label,parent,false);
        return new LabelHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof LabelHolder){
            LabelHolder labelHolder= (LabelHolder) holder;
            RelativeLayout.LayoutParams params=
                    (RelativeLayout.LayoutParams) labelHolder.tvLabel.getLayoutParams();
            labelHolder.tvLabel.setText(mLabelName);
            if(mPosition==0){
                params.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
                labelHolder.tvLabel.setBackgroundResource(R.drawable.shape_corner_8dp_tl_solid_ff9e27);
            }else if(mPosition==1){
                params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
                labelHolder.tvLabel.setBackgroundResource(R.drawable.shape_corner_8dp_tr_solid_ff9e27);
            }
            labelHolder.tvLabel.setLayoutParams(params);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return CreditFragment.TYPE_LABEL;
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    private class LabelHolder extends RecyclerView.ViewHolder{
        private TextView tvLabel;
        private LabelHolder(View itemView) {
            super(itemView);
            tvLabel= (TextView) itemView.findViewById(R.id.tv_label);
        }
    }
}
